package com.testmall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testmall.entity.User;
import com.testmall.mapper.UserMapper;
import com.testmall.service.UserService;
import com.testmall.utils.PasswordEncryptionUtil;
import com.testmall.utils.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
  * @Description:
  * @Author: littleDesire
  * @Date: 2022-06-21 14:29
  * @Version: 1.0
  *
  */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserIdUtil userIdUtil;
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户注册
     * @param user 用户对象
     * @return  插入的行数
     */
    @Override
    public int registerUser(User user) {
        /*
        * （串 + password + 串） ------ MD5算法进行加密，连续加载三次
        *（盐值 + password + 盐值） ------ 盐值就是一个随机字符串
        * */
        // 判断用户名是否存在
        User tempUser = userMapper.selectByUsername(user.getUsername());
        // 判断电话是否存在， 两着都必须唯一
        int i = userMapper.selectByPhone(user.getPhone());
        if(tempUser != null || i > 0){
            return 0;
        }
        // 获取盐值（随机生成一个盐值，保存到数据库当中）
        String salt = UUID.randomUUID().toString().toUpperCase();
        user.setSalt(salt);
        String password = user.getPassword();
        // 对密码进行加密
        String md5Password = PasswordEncryptionUtil.getMd5Password(password, salt);
        user.setPassword(md5Password);
        //找回密码的密保答案进行加密
        String md5Answer = PasswordEncryptionUtil.getMd5Password(user.getAnswer(), salt);
        user.setAnswer(md5Answer);
        // 设置优雅的UserId
        user.setUserid(userIdUtil.getShortUuid());
        return userMapper.registerUser(user);
    }

    /**
     * 用户登录
     * @param map 用户名和密码
     * @return 用户对象
     */
    @Override
    public int login(Map<String, Object> map) {
        // 查询用户是否存在
        User tempUser = null;
        if (null != map.get("username")) {
            tempUser = userMapper.selectByUsername(map.get("username").toString());
            if(tempUser == null){
                return 0;
            }
        }
        if (null != map.get("phone")){
            tempUser = userMapper.selectbyPhone2(map.get("phone").toString());
            if (tempUser == null) {
                return 0;
            }
        }
        // 对密码进行加密
        String password = (String) map.get("password");
        String md5Password = PasswordEncryptionUtil.getMd5Password(password, tempUser.getSalt());
        // 判断密码是否正确
        if(!md5Password.equals(tempUser.getPassword())){
            return 0;
        }
        map.put("password", md5Password);
        return userMapper.login(map);
    }

    @Override
    public int changePassword(Map<String, Object> map) {
        String newPassword = map.get("newPassword").toString();
        String oldPassword = map.get("oldPassword").toString();
        User tempUser = null;
        if (null != map.get("username")) {
            tempUser = userMapper.selectByUsername(map.get("username").toString());
            if(tempUser == null){
                return 0;
            }
        }
        if (null != map.get("phone")){
            tempUser = userMapper.selectbyPhone2(map.get("phone").toString());
            if (tempUser == null) {
                return 0;
            }
        }
        assert tempUser != null;
        // 对老密码进行加密
        String username = tempUser.getUsername();
        String md5PasswordOld = PasswordEncryptionUtil.getMd5Password(oldPassword, tempUser.getSalt());
        String md5PasswordNew;
        // 判老断密码是否正确
        if(!md5PasswordOld.equals(tempUser.getPassword())){
            return 0;
        }else {
            // 对新密码加密
            md5PasswordNew = PasswordEncryptionUtil.getMd5Password(newPassword, tempUser.getSalt());
        }
        return userMapper.changePassword(md5PasswordNew,username);
    }

    @Override
    public List<Map<String, Object>> queryUser() {
        return userMapper.queryUser();
    }
}
